package com.topica.network;

import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;

public class SoapCaller {

	public static String NAMESPACE = "http://mobi.topica.vn/tinhnv/Service_server/Webserver.php";
	public int call(String METHOD_NAME, List<PropertyInfo> pr) {

		String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME + "?wsdl";
		SoapObject mSoapObject = new SoapObject(NAMESPACE, METHOD_NAME);
		int res = 0;

		try {

			for (int i = 0; i < pr.size(); i++) {
				mSoapObject.addProperty(pr.get(i));

			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			envelope.setOutputSoapObject(mSoapObject);

			// HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ACTION);
			SoapTransporter httpTransport = new SoapTransporter(SOAP_ACTION);

			httpTransport.call(SOAP_ACTION, envelope);

			//SoapObject response = (SoapObject) envelope.bodyIn;
			//String nn = response.getProperty(0).toString();
			res = (Integer) envelope.getResponse();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public String getCourseInfo(String METHOD_NAME, List<PropertyInfo> pr) {

		String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME + "?wsdl";
		SoapObject mSoapObject = new SoapObject(NAMESPACE, METHOD_NAME);
		String res = "";

		try {

			for (int i = 0; i < pr.size(); i++) {
				mSoapObject.addProperty(pr.get(i));

			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			envelope.setOutputSoapObject(mSoapObject);

			// HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ACTION);
			SoapTransporter httpTransport = new SoapTransporter(SOAP_ACTION);

			httpTransport.call(SOAP_ACTION, envelope);

			//SoapObject response = (SoapObject) envelope.bodyIn;
			//String nn = response.getProperty(0).toString();
			res = (String) envelope.getResponse();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public String getString(String METHOD_NAME, List<PropertyInfo> pr) {

		String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME;
		SoapObject mSoapObject = new SoapObject(NAMESPACE, METHOD_NAME);
		String res = "";
		try {

			for (int i = 0; i < pr.size(); i++) {
				mSoapObject.addProperty(pr.get(i));
			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.setOutputSoapObject(mSoapObject);

			// HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ACTION);
			SoapTransporter httpTransport = new SoapTransporter(SOAP_ACTION);

			httpTransport.call(SOAP_ACTION, envelope);

			//SoapObject response = (SoapObject) envelope.bodyIn;
			res = (String) envelope.getResponse();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	

	public int updateMark(String METHOD_NAME, List<PropertyInfo> pr){
		String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME + "?wsdl";
		SoapObject mSoapObject = new SoapObject(NAMESPACE, METHOD_NAME);
		int returnValue = 0;
		try {

			for (int i = 0; i < pr.size(); i++) {
				mSoapObject.addProperty(pr.get(i));
			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.setOutputSoapObject(mSoapObject);

			// HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ACTION);
			SoapTransporter httpTransport = new SoapTransporter(SOAP_ACTION);

			httpTransport.call(SOAP_ACTION, envelope);

			//SoapObject response = (SoapObject) envelope.bodyIn;
			returnValue = (Integer) envelope.getResponse();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnValue;
	}

	public boolean updateCourse(String methodUpdateCourse, List<PropertyInfo> pList) {
		String SOAP_ACTION = NAMESPACE + "/" + methodUpdateCourse + "?wsdl";
		SoapObject mSoapObject = new SoapObject(NAMESPACE, methodUpdateCourse);
		boolean returnValue = false;
		try {
			for (int i = 0; i < pList.size(); i++) {
				mSoapObject.addProperty(pList.get(i));
			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.setOutputSoapObject(mSoapObject);

			// HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ACTION);
			SoapTransporter httpTransport = new SoapTransporter(SOAP_ACTION);

			httpTransport.call(SOAP_ACTION, envelope);

			//SoapObject response = (SoapObject) envelope.bodyIn;
			returnValue = (Boolean) envelope.getResponse();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnValue;
	}
}
